package pro.softzhang.algo.lc1700;

import pro.softzhang.algo.flag.knowledge.KDoublePointer;
import pro.softzhang.algo.flag.knowledge.KString;
import pro.softzhang.algo.flag.x.XClassic;
import pro.softzhang.algo.flag.x.XOptimal;

@KString
@XClassic
public class LC1768_MergeStringsAlternately {
    public static void main(String[] args) {

    }

    @KDoublePointer
    static class Solution {
        public String mergeAlternately(String word1, String word2) {
            int l1 = word1.length(), l2 = word2.length(), p1 = 0, p2 = 0;
            StringBuilder sb = new StringBuilder();
            while (p1 < l1 || p2 < l2) {
                if (p1 < l1) {
                    sb.append(word1.charAt(p1++));
                }
                if (p2 < l2) {
                    sb.append(word2.charAt(p2++));
                }
            }
            return sb.toString();
        }
    }

    /**
     * 单指针
     * 交替出现
     */
    @XOptimal
    static class Solution1 {
        public String mergeAlternately(String word1, String word2) {
            int l1 = word1.length(), l2 = word2.length(), p = 0;
            StringBuilder sb = new StringBuilder();
            while (p < l1 || p < l2) {
                if (p < l1) sb.append(word1.charAt(p));
                if (p < l2) sb.append(word2.charAt(p));
                p++;
            }
            return sb.toString();
        }
    }
}
